package com.winit.schedule.core.zk;

import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZKUtils {
    private static transient Logger log           = LoggerFactory.getLogger(ZKUtils.class);

    public static String createPath(ZooKeeper zk, String path, CreateMode createMode, List<ACL> acl) throws Exception {
        String[] list = path.split("/");
        String zkPath = "";
        for (String str : list) {
            if (str.equals("") == false) {
                zkPath = zkPath + "/" + str;
                if (zk.exists(zkPath, false) == null) {
                    return zk.create(zkPath, null, acl, createMode);
                } else {

                }
            }
        }
        return path;
    }

    public static String createPath(ZooKeeper zk, String path, byte[] data, CreateMode createMode, List<ACL> acl)
                                                                                                                 throws Exception {
        String[] list = path.split("/");
        String zkPath = "";
        for (String str : list) {
            if (str.equals("") == false) {
                zkPath = zkPath + "/" + str;
                if (zk.exists(zkPath, false) == null) {
                    log.info("-------- Zookeeper path[" + zkPath + "] start create");
                    return zk.create(zkPath, data, acl, createMode);
                } else {
                    log.info("-------- Zookeeper path[" + zkPath + "] already exists");
                }
            }
        }
        return path;
    }

    public static String createJobTrackerPath(ZooKeeper zk, String path, byte[] data, CreateMode createMode, List<ACL> acl)
            throws Exception {
        String[] list = path.split("/");
        String zkPath = "";

        String dev_path = path + "0000000000";
        Stat stat = zk.exists(dev_path, false);
        if (null != stat) {
            zk.delete(dev_path, stat.getVersion());
        }

        for (String str : list) {
            if (str.equals("") == false) {
                zkPath = zkPath + "/" + str;
                if (zk.exists(zkPath, false) == null) {
                    log.info("-------- Zookeeper path[" + zkPath + "] start create");
                    return zk.create(zkPath, data, acl, createMode);
                } else {
                    log.info("-------- Zookeeper path[" + zkPath + "] already exists");
                }
            }
        }
        return path;
    }

    /**
     * 特殊方法，非生产使用，请谨慎使用
     * @param zk
     * @param path
     * @param data
     * @param createMode
     * @param acl
     * @return
     * @throws Exception
     */
    public static String createPath_dev(ZooKeeper zk, String path, byte[] data, CreateMode createMode, List<ACL> acl)
            throws Exception {
        String[] list = path.split("/");
        String zkPath = "";
        for (String str : list) {
            if (str.equals("") == false) {
                zkPath = zkPath + "/" + str;
                Stat stat = zk.exists(zkPath, false);
                if (stat== null) {
                    log.info("-------- Zookeeper path[" + zkPath + "] start create");
                    return zk.create(zkPath, data, acl, createMode);
                } else {
                    if (path.equals(zkPath)) {
                        zk.delete(zkPath, stat.getVersion());
                        return zk.create(zkPath, data, acl, createMode);
                    }
                    log.info("-------- Zookeeper path[" + zkPath + "] already exists");
                }
            }
        }
        return path;
    }

    public static void addChildWatcher(ZooKeeper zk, String path, Watcher watcher) throws KeeperException,
                                                                                  InterruptedException {
        zk.getChildren(path, watcher);

    }
}
